<template>
	<view class="index-page with-tabbar">
		<!-- 1. 顶部自定义导航栏 -->
		<u-navbar leftIcon="" :fixed="true" :safeAreaInsetTop="true" :bgColor="bgColor" :placeholder="true">
			<view class="u-nav-slot flex-center" slot="left">
				<div class="fontS32">我的</div>

			</view>
			<view class="u-nav-slot flex-center" :style="{ 'margin-right': custom.width + 10 + 'px' }" slot="right">
				<div class="iconfont icon-kefu pr-20 pl-20 fontS42" @click="handleToChatRoom"></div>
				<div @click="toUser('/pages/setting/userInfo')" class="iconfont icon-shezhi pl-20 fontS42">
				</div>

			</view>
		</u-navbar>
		<!-- 页面主要内容区域 -->
		<view class="page-content" :style="{ paddingTop: headerHeight + 'px' }">

			<!-- 个人信息 -->
			<view class="flex align-center " @click="toUser('/pages/setting/userSet')">
				<image class="head-img" v-if="userInfo.avatar"
					:src="userInfo.avatar.indexOf('https://') > -1 ? userInfo.avatar : baseUrl + userInfo.avatar"
					alt="" />
				<image class="head-img" v-else
					:src="userInfo.sex === '0' ? 'https://apitest.yijiangongxiang.com/template/nan.png' : 'https://apitest.yijiangongxiang.com/template/nv.png'"
					alt="" />
				<div class="ml-28">
					<div class="fontS32 color-1 font-weight lh-44">{{ userInfo.realName || '未认证' }}</div>
					<div class="fontS24 color-1 mt-4" v-if="userInfo.userType === '02'">{{ userInfo.phonenumber }}</div>
					<div class="fontS24 color-1 mt-4" v-if="userInfo.userType === '03'">{{ userInfo.companyName || '未认证'
					}}
					</div>
				</div>
				<div class="iconfont icon-youjiantou ml-auto"></div>
			</view>
			<div v-if="userInfo.userType === '03'">
				<!-- 收藏的服务 -->
				<div class="serve mt-28 bor-r8">
					<div v-for="(item, index) in serveList" :key="index" class="flex-center item" @click="goPage(item)">
						<div class="topIcon img-42">
							<img class="img-42" :src="item.icon" alt="">
						</div>
						<div class="ml-10 fontS28 font-500">{{ item.title }}</div>
						<div class="ml-auto flex-center">
							<div>{{ item.num }}</div>
							<div class="ml-10 iconfont icon-youjiantou"></div>
						</div>
					</div>
				</div>
				<!-- 我的订单 -->
				<div class="function-box mt-30 bor-r8">
					<div class="flex-center lh-50">
						<div class=" fontS28 color-3 font-weight">我的订单</div>
						<div class="ml-auto flex-center color-6"
							@click="goPage('/pages/order/allOrder?tabIndex=0', 'all')">
							<div class="fontS24">全部订单</div>
							<div class="iconfont icon-youjiantou pl-10 pr-10"></div>
						</div>
					</div>
					<view class="grid grid1 mt-34">
						<view class="grid-item" v-for="(item, itemIndex) in orderListCom" :key="itemIndex"
							@click="goPage(item)">
							<div class="grid-icon1">
								<img class="grid-icon1" :src="item.icon" alt="">
								<div class="num flex-center align-center justify-center" v-if="item.num">
									<div>{{ item.num }}</div>
								</div>
							</div>

							<div class="grid-text mt-20 color-3">{{ item.title }}</div>
						</view>
					</view>
				</div>
			</div>
			<!-- 个人端 -->
			<div v-else>
				<!-- 积分 -->
				<div class="point mt-32" @click="toScore">
					<img class="point-bg" src="https://xingangtong.oss-cn-beijing.aliyuncs.com/user/20250626Jd4rcD.png"
						alt="">
					<div class="flex align-center point-info">当前积分：0</div>
				</div>

				<!-- 沟通过 -->
				<view class="grid mt-30">
					<view class="grid-item" v-for="(item, itemIndex) in tabList" :key="itemIndex"
						@click="goPositon(item)">
						<div class="fontS40 font-weight color-3">{{ item.num }}</div>
						<div class="grid-text mt-10 color-3">{{ item.title }}</div>
					</view>
				</view>

				<!-- 常用功能 -->
				<div class="function-box mt-30">
					<div class="lh-50 fontS28 color-3 font-weight">常用功能</div>
					<view class="grid mt-10">
						<view class="grid-item" v-for="(item, itemIndex) in functionList" :key="itemIndex"
							@click="goPage(item)">
							<img class="grid-icon" :src="item.icon" alt="">
							<div class="grid-text mt-10 color-3">{{ item.title }}</div>
						</view>
					</view>
				</div>
				<!-- 我的订单 -->
				<div class="function-box mt-30">
					<div class="flex-center lh-50">
						<div class=" fontS28 color-3 font-weight">我的订单</div>
						<div class="ml-auto flex-center color-6"
							@click="goPage('/pages/order/allOrder?tabIndex=0', 'all')">
							<div class="fontS24">全部订单</div>
							<div class="iconfont icon-youjiantou pl-10 pr-10"></div>
						</div>
					</div>
					<view class="grid grid1 mt-34">
						<view class="grid-item" v-for="(item, itemIndex) in orderList" :key="itemIndex"
							@click="goPage(item)">
							<div class="grid-icon1">
								<img class="grid-icon1" :src="item.icon" alt="">
								<div class="num flex-center align-center justify-center" v-if="item.num">
									<div>{{ item.num }}</div>
								</div>
							</div>

							<div class="grid-text mt-20 color-3">{{ item.title }}</div>
						</view>
					</view>
				</div>
			</div>
		</view>
		<bottom-tab-bar-com v-if="userInfo.userType === '03'" :tabIndexs="tabNum" :unRead="unRead" />
		<bottom-tab-bar v-else :tabIndexs="tabNum" :unRead="unRead" />
	</view>
</template>

<script>
import { getUserInfoApi } from "@/api/auth";

import { onLineResume } from '@/api/resume'
import { myOrderCount } from '@/api/order'
import { unReadCount } from '@/api/message'
import { companyServeActionList } from '@/api/serve.js'
import { appCompanyPostList } from '@/api/index'
import { getCustomerService } from '../../api/user';

export default {
	data() {
		return {
			unRead: 0,
			tabNum: 3,
			bgColor: 'transparent',
			baseUrl: getApp().globalData.baseUrl,
			headerHeight: 30, // 用于存储头部高度
			statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
			tabList: [
				{
					title: '沟通过',
					num: 0,
					status: 1,
				},
				{
					title: '看过我',
					num: 0,
					status: 2,
				},
				{
					title: '已投简历',
					num: 0,
					status: 3,
				},
				{
					title: '职位收藏',
					num: 0,
					status: 4,
				},
			],
			functionList: [
				{
					title: '收藏的服务',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/20250626Vg1Um2.png',
					url: '/pages/userList/collect'
				},
				{
					title: '在线简历',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/20250626nMr0hm.png',
					url: '/pages/resume/creat1',
					type: 'resume'
				},
				{
					title: '附件简历',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/202506265dnZRR.png',
					url: '/pages/resume/attachment',
				},
				{
					title: '求职意向',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/20250626H5qc7Y.png',
					url: '/pages/resume/creat5',
				},

			],
			orderList: [
				{
					title: '待付款',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/待付款@3x.png',
					url: '/pages/order/allOrder?tabIndex=1&typeName=待付款',
					num: 0
				},
				{
					title: '待开始',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/20250629eKv5yK.png',
					url: '/pages/order/allOrder?tabIndex=4&typeName=待开始',
					num: 0
				},

				{
					title: '服务中',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/服务中@3x.png',
					url: '/pages/order/allOrder?tabIndex=5&typeName=服务中',
					num: 0
				},
				{
					title: '已取消',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/202506294P0cWr.png',
					url: '/pages/order/allOrder?tabIndex=3&typeName=其他',
					num: 0
				},
				{
					title: '其他',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/其他@3x.png',
					url: '/pages/order/allOrder?tabIndex=99&typeName=其他',
					num: 0
				},
			],
			// 企业
			serveList: [
				{
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/收藏@3x.png',
					title: '收藏的服务',
					num: 1,
					url: '/pages/userList/collect'
				},
				{
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/积分.png',
					title: '积分',
					num: 0,
					url: '/pages/userList/score'
				},
				{
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/正在追@3x.png',
					title: '我的招聘',
					num: 0,
					url: '/pages/userList/myHire'
				},
			],
			orderListCom: [
				{
					title: '待付款',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/待付款@3x.png',
					url: '/pages/order/allOrder?tabIndex=1&typeName=待付款',
					num: 0
				},
				{
					title: '待签约',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/待签约@3x.png',
					url: '/pages/order/allOrder?tabIndex=2&typeName=待签约',
					num: 0
				},
				{
					title: '已签约',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/已签约@3x.png',
					url: '/pages/order/allOrder?tabIndex=4&typeName=已签约',
					num: 0
				},
				{
					title: '服务中',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/服务中@3x.png',
					url: '/pages/order/allOrder?tabIndex=5&typeName=服务中',
					num: 0
				},
				{
					title: '其他',
					icon: 'https://xingangtong.oss-cn-beijing.aliyuncs.com/user/其他@3x.png',
					url: '/pages/order/allOrder?tabIndex=99&typeName=其他',
					num: 0
				},
			],


			resumeDetail: null,
			custom: null,
			count: null,
			totalHire: 0,
			totalCollect: 0,
			customerService: null

		};
	},

	onPageScroll(e) {

		if (e.scrollTop > 1) {
			this.bgColor = '#fff'
		} else {
			this.bgColor = 'transparent'
		}

	},

	computed: {
		userInfo() {
			return this.$store.getters.userInfo || {};
		},

	},
	async onLoad() {
		this.custom = wx.getMenuButtonBoundingClientRect()


	},
	async onShow() {

		if (this.userInfo.userType === '03') {
			this.tabNum = 4
			await this.getCountCollect()
			await this.getCountHire()

		} else {
			this.tabNum = 3
		}
		this.$forceUpdate()

		if (this.userInfo.userId) {
			await this.getInfo()
			await this.getCount()
			await this.getUnReadCount()
		}

	},


	methods: {
		async handleToChatRoom() {
			if (this.userInfo.userType) {
				getCustomerService({ serviceType: 1 }).then(res => {
					console.log('serviceDetail ===>', res);
					const { userId, nickName } = res.data;
					if (userId) {
						uni.navigateTo({
							url: `/pages/chatroom/chatroom?toUserId=${userId}&nick=${nickName}`
						});
					}
				})
			} else {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}

		},
		async getCountCollect() {
			const { total } = await companyServeActionList({
				type: 4
			})
			this.serveList[0].num = total
		},
		async getCountHire() {
			const { total } = await appCompanyPostList({ companyId: this.userInfo.companyId })
			this.serveList[2].num = total
		},
		async getUnReadCount() {
			const { data } = await unReadCount()
			getApp().globalData.unReadCount = data
			this.unRead = data
		},
		toUser(url) {
			if (this.userInfo.userId) {
				uni.navigateTo({
					url
				})
			} else {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}

		},
		toScore() {
			uni.navigateTo({
				url: '/pages/userList/score'
			})
		},
		goPositon(item) {
			uni.navigateTo({
				url: `/pages/userList/position?status=${item.status}`
			})
		},
		async getCount() {
			console.log('getOCut')
			const { data } = await myOrderCount({
				channel: 1,
				userId: this.userInfo.userId,
				userType: this.userInfo.userType
			})
			console.log(data)
			if (this.userInfo.userType === '03') {
				this.orderListCom[0].num = data.waitPayCnt
				this.orderListCom[1].num = data.waitSignCnt
				this.orderListCom[2].num = data.signedCnt
				this.orderListCom[3].num = data.inServiceCnt
				// this.orderListCom[4].num = data.cancelCnt
			} else {
				this.orderList[0].num = data.waitPayCnt
				this.orderList[1].num = data.signedCnt
				// this.orderList[2].num = data.signedCnt
				this.orderList[2].num = data.inServiceCnt
				this.orderList[3].num = data.cancelCnt
			}
			//
			// this.count = data

		},
		async getonLineResume() {
			const { data } = await onLineResume()
			this.resumeDetail = data

		},
		async goPage(item, type) {
			if (this.userInfo.userId) {
				if (type === 'all') {
					uni.navigateTo({
						url: `${item}`
					})
				}

				if (item.type === 'resume') {
					await this.getonLineResume()
					if (this.resumeDetail && this.resumeDetail.name) {
						uni.navigateTo({
							url: `/pages/resume/myResume`
						})
					} else {
						uni.showModal({
							title: '提示',
							content: '您还没有在线简历，是否前去创建？',
							confirmColor: '#37AF71',
							success: async function (res) {
								if (res.confirm) {
									uni.navigateTo({
										url: `${item.url}`
									})
								} else if (res.cancel) {

								}
							}
						});


					}
				} else if (item.title === '我的招聘') {
					if (this.userInfo.companyId) {
						uni.navigateTo({
							url: `${item.url}`
						})
					} else {
						uni.showModal({
							title: '提示',
							content: '你还未认证企业是否去认证？',
							confirmColor: '#37AF71',
							success: async function (res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/auth/companyAuth'
									})
								} else if (res.cancel) {

								}
							}
						});
					}


				} else {
					uni.navigateTo({
						url: `${item.url}`
					})
				}
			} else {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		},
		async getInfo() {
			const data = await getUserInfoApi()
			// data.user.userType = this.userInfo.userType
			// this.$store.commit("SET_USERINFO", data.user);
			this.users = data.user
			this.users.userType = this.userInfo.userType
			this.$store.commit("SET_USERINFO", this.users);

		},

	}
};
</script>

<style lang="less" scoped>
@import url(user.less);
</style>
